function arrayToTree(items, parentId) {
  const result = [];
  const itemMap = {};
  items?.forEach((item) => {
    const id = item.id;
    const pid = item.parentId;
    if (!itemMap?.[id]) {
      itemMap[id] = {
        ...item,
        children: [],
      };
    }

    itemMap[id] = {
      ...item,
      children: itemMap[id]['children'],
    };
    if (pid === parentId) {
      result.push(itemMap[id]);
    } else {
      if (!itemMap?.[pid]) {
        itemMap[pid] = {
          ...item,
          children: [],
        };
      }
      itemMap[pid].children.push(itemMap[id]);
    }
  });
  return result;
}
export default arrayToTree;
